package cn.life.business.inspect.service.impl;


import cn.life.business.inspect.dao.InspectDao;
import cn.life.business.inspect.entity.po.Inspect;
import cn.life.business.inspect.entity.vo.InspectVo;
import cn.life.business.inspect.service.InspectService;
import cn.life.business.project.dao.ProjectDao;
import cn.life.business.project.entity.po.ProjectPo;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.push.PushService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 王振宇
 * @Description:
 * @Date 2018-07-26 10:41:55
 */
@Service
public class InspectServiceImpl  implements InspectService {

    @Autowired
    private InspectDao inspectDao;

    @Autowired
    private ProjectDao projectDao;

    @Override
    public CMessage addInspect(Inspect inspect) {
        ProjectPo projectPo = new ProjectPo();
        projectPo.setProjectId(inspect.getProjectId());
        projectPo = projectDao.queryProjectDetail(projectPo);
        inspect.setProjectName(projectPo.getProjectName());
        return inspectDao.addInspect(inspect)== 1 ?CMessage.success() : CMessage.fail("添加检测记录失败！");
    }

    @Override
    public CMessage deleteInspect(Inspect inspect) {
        return inspectDao.deleteInspect(inspect)== 1 ?CMessage.success() : CMessage.fail("删除检测记录失败！");
    }

    @Override
    public CMessage updateInspect(Inspect inspect) {
        return inspectDao.updateInspect(inspect)== 1 ?CMessage.success() : CMessage.fail("修改检测记录失败！");
    }

    @Override
    public CMessage selectInspect(InspectVo inspectVo) {
        Pagination<InspectVo> list=inspectDao.selectInspect(inspectVo);
        List<InspectVo> data = list.getRows();
        for(InspectVo  d : data){
            if(d.getStatus() == 2){
                if(d.getInspectEndState() == 0){
                    if(d.getInspectStatus() == 0){
                        //待整改
                        d.setInspectEndState(2);
                    }else {
                        //未整改
                        d.setInspectEndState(0);
                    }
                }else{
                    //已整改
                    d.setInspectEndState(1);
                }
            }
        }
        return CMessage.successByData(list);
    }

    @Override
    public CMessage getInspect(InspectVo inspectVo) {
        inspectVo=inspectDao.getInspect(inspectVo);
        return CMessage.successByData(inspectVo);
    }

    @Override
    public CMessage statistics(InspectVo inspectVo) {
        Pagination<InspectVo> list=inspectDao.statistics(inspectVo);
        return CMessage.successByData(list);
    }

    @Override
    public CMessage statisticsDetails(InspectVo inspectVo) {
        Pagination<InspectVo> list=inspectDao.statisticsDetails(inspectVo);
        return CMessage.successByData(list);
    }

    @Override
    public List<InspectVo> statisticsExcel(InspectVo inspectVo) {
        List<InspectVo> list=inspectDao.statisticsExcel(inspectVo);
        return list;
    }

    @Override
    public void sendInspectMessage() {
        //1.查询所有检测列表
        List<Map<String,Object>> messageList = inspectDao.queryInspectListByMessage();
        //2.如有当天需检测验证的信息则提醒
        List<String> jgList = new ArrayList<>();
        List<String> hwList = new ArrayList<>();
        List<String> iosList = new ArrayList<>();
        for(Map<String,Object> map : messageList){
            if(map.get("jg") != null && !map.get("jg").equals("")){
                jgList.add(map.get("userId").toString());
            }else  if(map.get("hw") != null && !map.get("hw").equals("")){
                hwList.add(map.get("userId").toString());
            }else  if(map.get("ios") != null && !map.get("ios").equals("")){
                iosList.add(map.get("userId").toString());
            }
        }
        PushService pushService = new PushService();
        if(jgList.size() > 0){
            pushService.pushVoInit("检测","您今天有未完成的检测，请进入检测模块进行查看。","jg",jgList);
        }
        if(hwList.size() > 0){
            pushService.pushVoInit("检测","您今天有未完成的检测，请进入检测模块进行查看。","hw",hwList);
        }
        if (iosList.size() > 0){
            pushService.pushVoInit("检测","您今天有未完成的检测，请进入检测模块进行查看。","ios",iosList);
        }
    }

}
